package com.lzx.iteam.net;

import android.content.Context;
import android.os.Message;
import android.util.Log;
import com.lzx.iteam.bean.TalkListData;
import com.lzx.iteam.util.Constants;
import com.lzx.iteam.util.PreferenceUtil;
import com.lzx.iteam.util.StringUtil;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Megan on 15/10/16.
 */
public class GetTalkListMsg extends CcMsgStructure {


    private Message mmCallback;
    private Context mContext;
    private PreferenceUtil mPreferenceUtil;
    private String userId;

    public GetTalkListMsg(Message callback, Context context) {
        mmCallback = callback;
        mContext = context;
        mPreferenceUtil = PreferenceUtil.getInstance(mContext);
        userId = mPreferenceUtil.getString(PreferenceUtil.CLIENT_USEID, "－1");
    }

    @Override
    public void onReceive(JSONObject data) {
        ArrayList<TalkListData> talkListDatas = new ArrayList<TalkListData>();
        try {
            JSONObject jsonObject = data.getJSONObject(AsynHttpClient.KEY_CC_DATA);
            JSONArray jsonArray=jsonObject.getJSONArray("talks");
            for (int k = 0; k < jsonArray.length(); k++) {
                TalkListData talkListData = new TalkListData();
                talkListData.allCounts=jsonObject.getString("counts");
                JSONObject item = jsonArray.getJSONObject(k);
                talkListData.talkId = item.getString("talk_id");
                talkListData.uName = item.getString("uname");
                talkListData.uId = item.getString("uid");
                talkListData.uImage = item.getString("uimg");
                talkListData.isPraise="0";
                if (item.has("talk")) {
                    JSONObject talk = item.getJSONObject("talk");
                    talkListData.text = talk.getString("text");
                    talkListData.updateTime = talk.getString("update_time");
                    talkListData.createTime = talk.getString("create_time");
                    if (talk.has("content")) {
                        JSONArray image = talk.getJSONArray("content");
                        StringBuilder thumbSb = new StringBuilder();
                        StringBuilder oriSb = new StringBuilder();
                        for (int i = 0; i < image.length(); i++) {
                            JSONObject imageObject = image.getJSONObject(i);
                            if (imageObject.has("thumb")) {
                                thumbSb.append(imageObject.getString("thumb") + ",");
                            }
                            if (imageObject.has("ori")) {
                                oriSb.append(imageObject.getString("ori") + ",");
                            }
                        }
                        if (thumbSb.length() > 0) {
                            talkListData.thumbImage = thumbSb.substring(0, thumbSb.length() - 1);
                        }
                        if (oriSb.length() > 0) {
                            talkListData.oriImage = oriSb.substring(0, oriSb.length() - 1);
                        }

                    }
                }
                if (item.has("comments")) {
                    ArrayList<Map<String,String>> praiseDatas = new ArrayList<Map<String,String>>();
                    ArrayList<Map<String,String>> commentDatas = new ArrayList<Map<String,String>>();
                    JSONArray comments = item.getJSONArray("comments");
                    for (int j = 0; j < comments.length(); j++) {
                        JSONObject commentObject = comments.getJSONObject(j);
                        Map<String,String> talkUserData = new HashMap<String, String>();
                        talkUserData.put(Constants.TALK_ID,commentObject.getString("talk_id"));
                        talkUserData.put(Constants.TALK_ABOUT_ID,commentObject.getString("_id"));
                        talkUserData.put(Constants.TALK_USER_ID,commentObject.getString("user_id"));
                        talkUserData.put(Constants.TALK_USER_NAME,commentObject.getString("user_name"));
                        talkUserData.put(Constants.TALK_USER_IMG,commentObject.getString("user_img"));

                        if (commentObject.has("reply_uid")) {
                            talkUserData.put(Constants.TALK_REPLY_ID,commentObject.getString("reply_uid"));
                        }
                        if (commentObject.has("reply_name")) {
                            talkUserData.put(Constants.TALK_REPLY_NAME,commentObject.getString("reply_name"));
                        }
                        talkUserData.put(Constants.TALK_TYPE,commentObject.getString("type"));
                        if (commentObject.has("comment")) {
                            talkUserData.put(Constants.TALK_COMMENT,commentObject.getString("comment"));
                        }
                        talkUserData.put(Constants.TALK_CREATE_TIME,commentObject.getString("create_time"));
                        if ("1".equals(commentObject.getString("type"))) {
                            praiseDatas.add(talkUserData);
                            if(talkUserData.get(Constants.TALK_USER_ID).equals(userId)){
                                talkListData.isPraise="1";
                                Log.d("get_talk_list_msg", talkListData.talkId + "    &&&&   " + talkListData.isPraise);
                            }
                        } else {
                            commentDatas.add(talkUserData);
                        }
                    }
                    talkListData.comments = commentDatas;
                    talkListData.praises = praiseDatas;
                }
                talkListDatas.add(talkListData);

            }


        } catch (JSONException e) {
            Log.e("JSONException",e.toString());
            e.printStackTrace();
        }


        if (mmCallback != null) {
            mmCallback.obj = talkListDatas;
            mmCallback.arg1 = 0;
            mmCallback.sendToTarget();
            return;
        }

    }

    @Override
    public void onError(int error, Object ret) {
        String errorMsg = null;
        if (ret != null) {
            JSONObject jData = null;
            try {
                jData = new JSONObject(ret.toString());
                errorMsg = jData.getString("msg");
            } catch (JSONException e) {
                errorMsg = "服务端数据解析失败!";
                e.printStackTrace();
            }

        }
        if (mmCallback != null) {
            mmCallback.arg1 = error;
            if (!StringUtil.isEmpty(errorMsg)) {
                mmCallback.obj = errorMsg;
            } else {
                mmCallback.obj = Constants.NETWORK_WRROR;
            }
            mmCallback.sendToTarget();
        }
    }
}